//
//  ZhuanRangViewController.m
//  TenThousand
//
//  Created by 我的 on 16/7/22.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "ZhuanRangViewController.h"

#import "TransferGetListDetailModel.h"//承接债权明细model
#import "WelcomeLoginViewController.h"
#import "CapitalSubsidiaryViewController.h"


@interface ZhuanRangViewController ()<UIAlertViewDelegate>
{
    NSString *_WaterNumber;
}
/**预期年化*/
@property (weak, nonatomic) IBOutlet UILabel *rate;
/**产品项目*/
@property (weak, nonatomic) IBOutlet UILabel *prjectName;
/**待收收益*/
@property (weak, nonatomic) IBOutlet UILabel *totalAmount;
/**待收本金*/
@property (weak, nonatomic) IBOutlet UILabel *collected;
/**待收利息*/
@property (weak, nonatomic) IBOutlet UILabel *interest;
/**借款标详细*/
@property (weak, nonatomic) IBOutlet UILabel *nameDetail;

@property (weak, nonatomic) IBOutlet UIButton *chengjieBtn;
@property (weak, nonatomic) IBOutlet UILabel *Enddate;//回款日期
@property (weak, nonatomic) IBOutlet UILabel *huanKuaiMoney;//还款金额

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ZhuanRangViewController
{
    TransferGetListDetailModel *_detailModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.projectName;
    
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
   
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)setData{
    
    _rate.text = [NSString stringWithFormat:@"%.2f",_model.Rate];
    _prjectName.text = [NSString stringWithFormat:@"%@%@", _detailModel.PrjectName, _detailModel.Code];
    _totalAmount.text = [NSString stringWithFormat:@"%.2f",[_detailModel.TotalAmount doubleValue]];
    _collected.text = _detailModel.collected;
    _interest.text = _detailModel.interest;
    _nameDetail.text = _detailModel.NameDetail;
    _Enddate.text = _detailModel.Enddate;
    self.huanKuaiMoney.text = [NSString stringWithFormat:@"支付金额：%.2f元", [_detailModel.Ammount doubleValue]];
    
    
   

    //判断状态为取消或者已转让 按钮zhihui
    if ([_detailModel.State isEqualToString:@"取消"] || [_detailModel.State isEqualToString:@"已转让"] ) {
        _chengjieBtn.enabled = NO;
    } else {
        _chengjieBtn.enabled = YES;
    }

}

- (IBAction)transerBtn:(id)sender {
    
    [self getAddafterTransfer];
}

- (void)FengFu:(NSString *)requestId {
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSString *subledgerList = [NSString stringWithFormat:@"[{\"roleType\":\"0\",\"roleCode\":\"%@\",\"inOrOut\":\"0\",\"sum\":\"%.2f\",\"bizFlag\":\"0\"}]", _detailModel.TLoginId, [_detailModel.Ammount doubleValue]];
    NSLog(@"%@======",_borrowModel.WaterNumber);
    NSDictionary *dic = @{
                          @"requestType":@"PFT0012",
                          @"requestId":requestId,
                          @"merchantCode":FengFuMerchantCode,
                          @"userIdIdentity":loginId,
                          @"projectCode":self.Borrowid,
                          @"projectDescription":@"",
                          @"originalRequestId":_detailModel.oldWaterNumber,
                          @"originalOrderSum":[NSString stringWithFormat:@"%.2f", [_detailModel.collected doubleValue]],
                          @"assignmentSum":[NSString stringWithFormat:@"%.2f", [_detailModel.Ammount doubleValue]],
                          @"undertakeSum":[NSString stringWithFormat:@"%.2f", [_detailModel.Ammount doubleValue]],
                          @"payType":@"3",
                          @"subledgerList":subledgerList,
                          @"creditValue":@"",
                          @"undertakePercentage":@"",
                          @"mainAccountType":@"",
                          @"mainAccountCode":@"",
                          @"noticeUrl":[NSString stringWithFormat:@"%@API/notice/CreditAssignment.aspx ", noticeUrl],
                          @"successReturnUrl":@"",
                          @"failReturnUrl":@"",
                          };
    NSString *sign = [[[[[[[[[[[[[[[[[[dic[@"requestType"] stringByAppendingString:dic[@"requestId"]] stringByAppendingString:dic[@"merchantCode"]] stringByAppendingString:dic[@"userIdIdentity"]] stringByAppendingString:dic[@"projectCode"]] stringByAppendingString:dic[@"projectDescription"]] stringByAppendingString:dic[@"originalRequestId"]] stringByAppendingString:dic[@"originalOrderSum"]] stringByAppendingString:dic[@"assignmentSum"]] stringByAppendingString:dic[@"undertakeSum"]] stringByAppendingString:dic[@"payType"]] stringByAppendingString:dic[@"subledgerList"]] stringByAppendingString:dic[@"creditValue"]] stringByAppendingString:dic[@"undertakePercentage"]] stringByAppendingString:dic[@"mainAccountType"]] stringByAppendingString:dic[@"mainAccountCode"]] stringByAppendingString:dic[@"noticeUrl"]] stringByAppendingString:dic[@"successReturnUrl"]] stringByAppendingString:dic[@"failReturnUrl"]];
    
    NSString *signature = [HMACMD5 HMACMD5WithString:sign WithKey:FengFuKEY];
    NSDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [dict setValue:signature forKey:@"signature"];
    [SumaPaySDK startService:dict callback:^(id resultDic, NSError *error) {
        NSLog(@"%@--------------%@", resultDic, error);
        NSDictionary *dic = [NetWorkManager dictionaryWithJsonString:resultDic];
        if ([dic[@"result"] integerValue] == 0) {
            if ([dic[@"msg"][@"result"] integerValue] == 00000) {
                [self getSureTransfer:requestId];
            } else {
                
                [self.navigationController popViewControllerAnimated:YES];
            }
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

//转让接口服务
-(void)transferGetList{
    
    if (self.TransferId == nil && self.Borrowid == nil) {
        return;
    }else {
        
        NSDictionary *para = @{
                               @"TransferId" : self.TransferId,
                               @"Borrowid" : self.Borrowid,
                               
                               };
        [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Transfer.GetListDetail" withParaments:para withSuccessBlock:^(NSDictionary *object) {
            NSLog(@"%@", object);
            NSDictionary *dic = object[@"Data"][0];
            _detailModel = [[TransferGetListDetailModel alloc] init];
            [_detailModel setValuesForKeysWithDictionary:dic];
            
            [self.dataArr addObject:_detailModel];
            
            [self setData];
            
        } withFailureBlock:^(NSError *error) {
            NSLog(@"%@",error);
        } progress:nil];

    }

    
}

//债权转让调用丰付后接口
- (void)getSureTransfer:(NSString *)requestId {
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSDictionary *para = @{
                           @"WaterNumber":requestId,
                           @"AssignmentSum":_detailModel.TotalAmount,
                           @"LoginId":loginId
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Transfer.GetSureTransfer" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
        [ToastView showInfoWithStatus:object[@"Message"]];
        [self transferGetList];
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } progress:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未登录，请先登录" delegate: self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 10000;
        [alert show];
        return;
    }
    
    [self transferGetList];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1 && alertView.tag == 10000) {
        WelcomeLoginViewController *loginView = [[WelcomeLoginViewController alloc] init];
        loginView.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginView animated:NO];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//债权转让调丰付前接口
-(void)getAddafterTransfer{
    
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSDictionary *para = @{
                           @"Dedit":_detailModel.Ammount,
                           @"Borrowid":self.Borrowid,
                           @"Traid":_detailModel.TransferId,
                           @"LoginId":loginId
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Transfer.GetAddafterTransfer" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",[object[@"Data"] firstObject][@"WaterNumber"]);
        NSString *WaterNumber = [object[@"Data"] firstObject][@"WaterNumber"];
        [self FengFu:WaterNumber];
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } progress:nil];
}
//项目介绍
- (IBAction)jieshao:(id)sender {
    
    CapitalSubsidiaryViewController *capitalView = [[CapitalSubsidiaryViewController alloc] init];
    capitalView.Borrowid = self.Borrowid;
    capitalView.num = 2;
    capitalView.Code = _detailModel.Code;
    capitalView.ProjectName = _detailModel.PrjectName;
    [self setHidesBottomBarWhenPushed:YES];
    [capitalView setSelectIndex: 0];
    [self.navigationController pushViewController:capitalView animated:YES];
}
//投资记录
- (IBAction)jilu:(id)sender {
    
    CapitalSubsidiaryViewController *capitalView = [[CapitalSubsidiaryViewController alloc] init];
    capitalView.Borrowid = self.Borrowid;
    capitalView.num = 2;
    capitalView.Code = _detailModel.Code;
    capitalView.ProjectName = _detailModel.PrjectName;
    [self setHidesBottomBarWhenPushed:YES];
    [capitalView setSelectIndex: 1];
    [self.navigationController pushViewController:capitalView animated:YES];
}
//债权承接协议
- (IBAction)xieyi:(id)sender {
    
    CapitalSubsidiaryViewController *capitalView = [[CapitalSubsidiaryViewController alloc] init];
    capitalView.Borrowid = self.Borrowid;
    capitalView.num = 2;
    capitalView.Code = _detailModel.Code;
    capitalView.ProjectName = _detailModel.PrjectName;
    [self setHidesBottomBarWhenPushed:YES];
    [capitalView setSelectIndex: 2];
    [self.navigationController pushViewController:capitalView animated:YES];
}
@end
